package com.liuzhongni.wahahanews.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.net.Uri;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.VideoView;

import com.android.volley.toolbox.NetworkImageView;
import com.liuzhongni.wahahanews.R;
import com.liuzhongni.wahahanews.singleton.VolleySingleton;
import com.liuzhongni.wahahanews.videoclass.VideoFirst;

/**
 * Created by dllo on 16/1/14.
 */
public class ListViewAdapterOfVideo extends BaseAdapter {
    private VideoFirst videoFirst;
    private Context context;
    public ListViewAdapterOfVideo(VideoFirst videoFirst, Context context) {
        this.videoFirst = videoFirst;
        this.context = context;
    }

    @Override
    public int getCount() {
        return 10;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final VideoViewHolder holder;

        if (null == convertView) {
            convertView = LayoutInflater.from(context).inflate(R.layout.video_listview_item, null);
            holder = new VideoViewHolder(convertView);
            convertView.setTag(holder);
        } else {
            holder = (VideoViewHolder) convertView.getTag();
        }
        if(null==videoFirst.getVideoList()) {
            Log.d("ListViewAdapterOfVideo", "不存在");
        }else if(null==holder.imageViewqianjing) {
            Log.d("ListViewAdapterOfVideo", "前景不存在");
        }else if(null==VolleySingleton.getOurInstance()) {
            Log.d("ListViewAdapterOfVideo", "蛋类不存在");
        }
        holder.position = position;
        Log.d("ListViewAdapterOfVideo", "holder.position:" + holder.position);
        holder.imageViewqianjing.requestLayout();
        VolleySingleton.getOurInstance().imgJson(holder.imageViewqianjing, videoFirst.getVideoList().get(holder.position).getCover());
        holder.imageViewbofang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                setVideo(holder.videoView, videoFirst.getVideoList().get(holder.position).getMp4_url());
                holder.videoView.start();
                holder.imageViewqianjing.setVisibility(View.INVISIBLE);
                holder.imageViewbofang.setVisibility(View.INVISIBLE);
            }
        });
        holder.videoView.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
            @Override
            public void onCompletion(MediaPlayer mp) {
                holder.imageViewqianjing.setVisibility(View.VISIBLE);
                holder.imageViewbofang.setVisibility(View.VISIBLE);
            }
        });
        holder.textViewTitle.setText(videoFirst.getVideoList().get(holder.position).getTitle());
        holder.textViewDigest.setText(videoFirst.getVideoList().get(holder.position).getDescription());
        holder.textViewPlaytime.setText(timeChange(videoFirst.getVideoList().get(holder.position).getLength()));
        holder.textViewPlaycount.setText(videoFirst.getVideoList().get(holder.position).getPlayCount() + "");
        holder.imageViewFenxiang.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "分享", Toast.LENGTH_SHORT).show();

            }
        });
        holder.imageViewLiuyan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(context, "留言", Toast.LENGTH_SHORT).show();
            }
        });
        return convertView;
    }

    public class VideoViewHolder {
        private VideoView videoView;
        private TextView textViewTitle;
        private TextView textViewDigest;
        private TextView textViewPlaytime;
        private TextView textViewPlaycount;
        private ImageView imageViewLiuyan;
        private ImageView imageViewFenxiang;
        private NetworkImageView imageViewqianjing;
        private ImageView imageViewbofang;
        private int position;

        public VideoViewHolder(View view) {

            videoView = (VideoView) view.findViewById(R.id.video_lv_item_vv);
            textViewTitle = (TextView) view.findViewById(R.id.video_lv_item_title);
            textViewDigest = (TextView) view.findViewById(R.id.video_lv_item_digest);
            textViewPlaytime = (TextView) view.findViewById(R.id.video_lv_item_playtime);
            textViewPlaycount = (TextView) view.findViewById(R.id.video_lv_item_playcount);
            imageViewLiuyan = (ImageView) view.findViewById(R.id.video_lv_item_liuyan);
            imageViewFenxiang = (ImageView) view.findViewById(R.id.video_lv_item_fenxiang);
            imageViewqianjing = (NetworkImageView) view.findViewById(R.id.video_lv_item_iv_qianjing);
            imageViewbofang = (ImageView) view.findViewById(R.id.video_lv_item_iv_bofang);
        }

    }

    public void setVideo(final VideoView videoView, String url) {
        videoView.setVideoURI(Uri.parse(url));
        MediaController mediaController = new MediaController(context);
        videoView.setMediaController(mediaController);

//        if(mediaController.isPressed()){
//            Toast.makeText(context, "wahaha", Toast.LENGTH_SHORT).show();
//
//        }

//        videoView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//
//            }
//        });

    }

    public String timeChange(int time) {
        int second = 0;
        int minite = 0;
        minite = time / 60;
        second = time % 60;
        if (minite < 10 && second < 10) {
            return "0" + minite + " : 0" + second;
        }
        if (minite < 10 && second >= 10) {
            return "0" + minite + " : " + second;
        }
        if (minite >= 10 && second < 10) {
            return minite + " : 0" + second;
        }
        if (minite >= 10 && second >= 10) {
            return minite + " : " + second;
        }
        return null;
    }

}
